﻿using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour {

    public LineRenderer line;
	public float forceAmount;
	public float maxAmount;

    private int layerMask = (1 << 7) | (1 << 8);

    public bool hasArrow = true;

    private Vector3 lastTarget;

	private float force = 4f;

	private Movement _move;

	private bool cancelPressed;
	private AnimationManager anim;

	public Transform top;
	public Transform bottom;

	public Camera cam;

	void Start()
	{
		Time.timeScale = 1;
		gameObject.AddComponent("FadeIn");
		GetComponent<FadeIn>().createFade(2f);
		_move = GetComponent<Movement>();
		anim = GetComponent<AnimationManager>();
	}

    void Update () 
    {
		if(Input.GetMouseButtonDown(1))
		{
			cancelPressed = true;
			StartCoroutine(closeEffect());


		}
		
        if (Input.GetMouseButton(0))
        {
			if(!cancelPressed && hasArrow)
			{
				RaycastHit2D hit = Physics2D.Linecast(transform.position, cam.ScreenToWorldPoint(Input.mousePosition), layerMask);

				_move.enabled = false;

	            if (hit.collider != null)
	            {
					Vector3 linePosition = transform.position;
					linePosition.y += 0.3f;

					line.SetPosition(0, linePosition);
	                line.SetPosition(1, hit.point);
	                lastTarget = new Vector3(hit.point.x, hit.point.y, 0);

					if(force < maxAmount)
						force += forceAmount*Time.deltaTime;
					float valueScale = top.localScale.y;
					
					if(top.localScale.y < 4)
						valueScale += (forceAmount/2)*Time.deltaTime;
					
					top.localScale = new Vector3(top.localScale.x,valueScale,top.localScale.z);
					bottom.localScale = new Vector3(top.localScale.x,valueScale,top.localScale.z);

	            }
	            else
	            {
					Vector3 linePosition = transform.position;
					linePosition.y += 0.3f;
					
					line.SetPosition(0, linePosition);
					line.SetPosition(1, cam.ScreenToWorldPoint(Input.mousePosition));
					lastTarget = cam.ScreenToWorldPoint(Input.mousePosition);

					if(force < maxAmount)
						force += forceAmount*Time.deltaTime;
	
					float valueScale = top.localScale.y;

					if(top.localScale.y < 4)
						valueScale += (forceAmount/2)*Time.deltaTime;

					top.localScale = new Vector3(top.localScale.x,valueScale,top.localScale.z);
					bottom.localScale = new Vector3(top.localScale.x,valueScale,top.localScale.z);

	            }

				if(hasArrow)
				{
					Vector2 mousePoint = cam.ScreenToWorldPoint(Input.mousePosition);
					float angleBetween = Mathf.Atan2(mousePoint.y - transform.position.y, mousePoint.x - transform.position.x) * 180 / Mathf.PI;

					if(angleBetween > -45 && angleBetween < 45)
					{
						anim.StartAnim("Fire_Right");
						anim.Stop();
						anim.ChangeFrame(1);
					}
					if(angleBetween < 135 && angleBetween > 45)
					{
						anim.StartAnim("Fire_Back");
						anim.Stop();
						anim.ChangeFrame(1);
					}
					if(angleBetween < -45 && angleBetween > -135)
					{
						anim.StartAnim("Fire_Front");
						anim.Stop();
						anim.ChangeFrame(1);
					}
					if(angleBetween < -135 || angleBetween > 135)
					{
						anim.StartAnim("Fire_Left");
						anim.Stop();
						anim.ChangeFrame(1);
					}
				}
			}
        }
        else if (Input.GetMouseButtonUp(0))
        {
			line.SetPosition(0, Vector3.zero);
			line.SetPosition(1, Vector3.zero);

			if(!cancelPressed)
			{

	            if (hasArrow)
	            {
	                GameObject arrow = Instantiate(Resources.Load("Prefabs/Arrow")) as GameObject;
	                arrow.GetComponent<ArrowBehaviour>().destinity = lastTarget;
					arrow.GetComponent<ArrowBehaviour>().arrowSpeed = force;
					arrow.transform.position = transform.position;
	                arrow.GetComponent<ArrowBehaviour>().InsertAngle();

					hasArrow = false;
					anim.Play();
					force = 4f;
					StartCoroutine(closeEffect());
	            }

				gameObject.SendMessage("RemoveArrow",SendMessageOptions.DontRequireReceiver);
			}
			else
				cancelPressed = false;
			_move.enabled = true;
		}
    }

	IEnumerator closeEffect()
	{
		while(true)
		{
			float valueScale = top.localScale.y;
			
			if(top.localScale.y > 2)
				valueScale -= (forceAmount*2)*Time.deltaTime;
			else
				break;

			top.localScale = new Vector3(top.localScale.x,valueScale,top.localScale.z);
			bottom.localScale = new Vector3(top.localScale.x,valueScale,top.localScale.z);

			yield return null;
		}
	}

    void ArrowRecovered()
    {
        hasArrow = true;
    }

}
